import { ossPolicy } from '@/common/service';

export const beforeUpload = (file, options) => {
  return new Promise((resolve, reject) => {
    ossPolicy()
      .then((res) => {
        const { host, accessKeyId, policy, signature, dir } = res;
        const suffix = file.name.slice(file.name.lastIndexOf('.'));
        const filename = Date.now() + suffix;
        const fileUrl = `${dir}/${filename}`;
        options.action = `${host}`;
        options.data = {
          key: fileUrl,
          policy,
          OSSAccessKeyId: accessKeyId,
          signature,
        };
        file.tempUrl = `${host}/${fileUrl}`;
        resolve(options);
      })
      .catch((e) => reject(e));
  });
};
